package com.ruoyi.terminal.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.terminal.domain.DcSim;
import com.ruoyi.terminal.service.IDcSimService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * sim卡管理Controller
 * 
 * @author ruoyi
 * @date 2021-01-08
 */
@RestController
@RequestMapping("/sim")
public class DcSimController extends BaseController
{
    @Autowired
    private IDcSimService dcSimService;

    /**
     * 查询sim卡管理列表
     */
    @PreAuthorize(hasPermi = "terminal:sim:list")
    @GetMapping("/list")
    public TableDataInfo list(DcSim dcSim)
    {
        startPage();
        List<DcSim> list = dcSimService.selectDcSimList(dcSim);
        return getDataTable(list);
    }

    /**
     * 导出sim卡管理列表
     */
    @PreAuthorize(hasPermi = "terminal:sim:export")
    @Log(title = "sim卡管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DcSim dcSim) throws IOException
    {
        List<DcSim> list = dcSimService.selectDcSimList(dcSim);
        ExcelUtil<DcSim> util = new ExcelUtil<DcSim>(DcSim.class);
        util.exportExcel(response, list, "sim");
    }

    /**
     * 获取sim卡管理详细信息
     */
    @PreAuthorize(hasPermi = "terminal:sim:query")
    @GetMapping(value = "/{simIccid}")
    public AjaxResult getInfo(@PathVariable("simIccid") String simIccid)
    {
        return AjaxResult.success(dcSimService.selectDcSimById(simIccid));
    }

    /**
     * 新增sim卡管理
     */
    @PreAuthorize(hasPermi = "terminal:sim:add")
    @Log(title = "sim卡管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DcSim dcSim)
    {
        return toAjax(dcSimService.insertDcSim(dcSim));
    }

    /**
     * 修改sim卡管理
     */
    @PreAuthorize(hasPermi = "terminal:sim:edit")
    @Log(title = "sim卡管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DcSim dcSim)
    {
        return toAjax(dcSimService.updateDcSim(dcSim));
    }

    /**
     * 删除sim卡管理
     */
    @PreAuthorize(hasPermi = "terminal:sim:remove")
    @Log(title = "sim卡管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{simIccids}")
    public AjaxResult remove(@PathVariable String[] simIccids)
    {
        return toAjax(dcSimService.deleteDcSimByIds(simIccids));
    }

    /**
     * 获取sim卡信息
     */
//    @PreAuthorize(hasPermi = "terminal:sim:refresh")
    @GetMapping(value = "/refresh/{simIccid}")
    public AjaxResult refreshSim(@PathVariable("simIccid") String[] simIccids)
    {
        for(int i=0; i<simIccids.length; i++)
        {dcSimService.updateDcSim(dcSimService.refreshSim(simIccids[i]));}
        return AjaxResult.success();
    }
}
